import pymysql;

'''
Mysql util
'''
__author__ = 'HymanHu';

def get_connectin():
    conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='test', charset='utf8');
    cur = conn.cursor();
    return conn, cur;

def execute_query(cur, sql):
    cur.execute(sql);
    return cur.fetchall();

def execute_edit(cur, sql):
    return cur.execute(sql);

def commit_tran(conn):
    if conn:
        conn.commit();

def rollback_(conn):
    if conn:
        conn.rollback();

def close_conn_cur(conn, cur):
    if conn:
        conn.close();
    if cur:
        cur.close();

def execute_(sql):
    conn, cur = get_connectin();
    result = None;
    if sql.lower().startswith("select"):
        result = execute_query(cur, sql);
    else:
        result = execute_edit(cur, sql);
        commit_tran(conn);
    close_conn_cur(conn, cur);
    return result;


if __name__ == "__main__":
    pass;